package cst.a706gdy.zyfypt_706gdy.model;

import android.content.SharedPreferences;

import java.util.List;

import cst.a706gdy.zyfypt_706gdy.bean.ArticleBean;
import cst.a706gdy.zyfypt_706gdy.bean.ProjectBean;
import cst.a706gdy.zyfypt_706gdy.bean.SVideoBean;
import cst.a706gdy.zyfypt_706gdy.bean.TcaseBean;
import cst.a706gdy.zyfypt_706gdy.bean.twareBean;
import cst.a706gdy.zyfypt_706gdy.service.ResService;
import cst.a706gdy.zyfypt_706gdy.bean.Loginbean;
import cst.a706gdy.zyfypt_706gdy.bean.VideoBean;
import cst.a706gdy.zyfypt_706gdy.iface.AmicoolIface;
import cst.a706gdy.zyfypt_706gdy.listener.AmicoolListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by DELL on 2017/5/8.
 */

public class ResModel<T> implements AmicoolIface {


    private Retrofit retrofit;
    private ResService service;
    private SharedPreferences sp;

    /*private static final String SESSION_ID="nkgfc8ag6jq69e2uepq8u2h242";*/
    private String SESSION_ID;
    private static final String BASE_URL = "http://amicool.neusoft.edu.cn/";

    public ResModel(String type, String s_id) {
        if (type.equals("gson")) {
            retrofit = new Retrofit.
                    Builder().
                    baseUrl(BASE_URL).
                    addConverterFactory(GsonConverterFactory.create()).
                    build();
        } else {
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(ResService.class);

        SESSION_ID = s_id;
    }

    public ResModel(String type) {
        if (type.equals("gson")) {
            retrofit = new Retrofit.//创建retrofit对象
                    Builder().
                    baseUrl(BASE_URL).
                    addConverterFactory(GsonConverterFactory.create()).
                    build();
        } else {
            retrofit = new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(ResService.class);

        SESSION_ID = "nkgfc8ag6jq69e2uepq8u2h242";
    }

    @Override
    public void login(String u, String p, final AmicoolListener l) {
        Call<Loginbean> call = service.login(u, p);//创建访问api的请求
        call.enqueue(new Callback<Loginbean>() { //发送请求并处理结果
            @Override
            public void onResponse(Call<Loginbean> call, Response<Loginbean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Loginbean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });

    }

    @Override
    public void reg(String u, String realname, int roleid, String p, String re_p, String email, String t, final AmicoolListener l) {
        Call<String> call = service.reg(u, realname, roleid, p, re_p, email, t);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());

            }
        });
    }

    @Override
    public void getList(String mod, int page, final AmicoolListener l) {
        switch (mod) {
            case "video":
                Call<List<VideoBean>> call = service.getVideoList(mod, page, SESSION_ID);
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });

                break;
            case "article":
                Call<List<ArticleBean>> call_a = service.getArticleList(mod, page, SESSION_ID);
                call_a.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tware":
                Call<List<twareBean>> call_p = service.gettwareList(mod, page, SESSION_ID);
                call_p.enqueue(new Callback<List<twareBean>>() {
                    @Override
                    public void onResponse(Call<List<twareBean>> call, Response<List<twareBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<twareBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "svideo":
                Call<List<SVideoBean>> call_s = service.getSVideoList("video", page, SESSION_ID);
                call_s.enqueue(new Callback<List<SVideoBean>>() {
                    @Override
                    public void onResponse(Call<List<SVideoBean>> call, Response<List<SVideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<SVideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });

                break;
            case "tcase":
                Call<List<TcaseBean>> call_t = service.getTcaseList(mod, page, SESSION_ID);
                call_t.enqueue((new Callback<List<TcaseBean>>() {
                    @Override
                    public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                }));
                break;
            case "project":
                Call<List<ProjectBean>> call_j = service.getPorjectList("project", page, SESSION_ID);
                call_j.enqueue((new Callback<List<ProjectBean>>() {
                    @Override
                    public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                }));
                break;
            default:
                break;
        }

    }

    @Override
    public void getDetail(String mod, int id, String SessionID, AmicoolListener l) {

    }

    @Override
    public void logout(String sessionID, AmicoolListener l) {

    }


    @Override
    public void getUserList(String mod, int page, String sessionID, int userid, AmicoolListener l) {

    }
}